package zr.dao;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;

import zr.Main;
import zr.component.PlayerInfoPane;
import zr.util.Logger;
import zr.util.UIExtends;

public class QiZi {
	private final static org.apache.log4j.Logger logger = Logger
			.getInstance(QiZi.class);

	public static final int WIDTH = 50;
	public static final int HEIGHT = 30;

	QiZiName name;
	public Color color;
	public JButton button;
	QiZiStatus status = null;
	Font font = new Font("微软雅黑", 1, 15);
	public Point point = new Point();

	public QiZi(QiZiName name, Color color) {
		this.name = name;
		this.color = color;
		this.button = new JButton(name.toString());
		this.button.setSize(WIDTH, HEIGHT);
		this.button.setForeground(color);
		this.button.setFont(font);
		this.button.setMargin(new Insets(0, 0, 0, 0));
		this.changeStatus(false);
		this.button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (Main.myTurn == true) {
					if (Main.currentSelectQizi == null) {// 当前没有任何棋子被选中，这时应更改被点棋子的状态
						changeStatus(true);
					} else {// 当前有棋子被选中
						if (Main.currentSelectQizi.equals(QiZi.this))// 如果所点棋子就是当前被选中的棋子，则应更改被点棋子的状态
							changeStatus(false);
						else if (Main.me.color
								.equals(Main.currentSelectQizi.color)) {
							// 点的是其它棋子（且此时的被选中棋子是己方的）
							if (Main.currentSelectQizi.color
									.equals(QiZi.this.color)) {//点的是己方的
								UIExtends.unflash(Main.currentSelectQizi.button);
								try {
									Thread.sleep(410);
								} catch (InterruptedException e1) {
									e1.printStackTrace();
								}
								QiZi.this.changeStatus(false);
							} else {//点的是对方的，说明是“吃子”或“并子”操作，则要判断能不能进行此操作
								if (QiZi.this.status.equals(QiZiStatus.扣着的))
									changeStatus(true);
								else if (Operation.canEat(
										Main.currentSelectQizi, QiZi.this) == true) {// 能吃
									Main.write(Operation.吃子 + ":["
											+ Main.currentSelectQizi.point.x
											+ ","
											+ Main.currentSelectQizi.point.y
											+ "][" + QiZi.this.point.x + ","
											+ QiZi.this.point.y + "]");
									Main.currentSelectQizi.moveTo(QiZi.this
											.getPoint());
									QiZi.this.destroy();

									Main.currentSelectQizi = null;
								} else if (Operation.canBing(
										Main.currentSelectQizi, QiZi.this) == true) {// 能并
									Main.write(Operation.并子 + ":["
											+ Main.currentSelectQizi.point.x
											+ ","
											+ Main.currentSelectQizi.point.y
											+ "][" + QiZi.this.point.x + ","
											+ QiZi.this.point.y + "]");
									QiZi.this.destroy();
									Main.currentSelectQizi.destroy();
									Main.qp.updateUI();
									Main.currentSelectQizi = null;
								} else
									// 什么都不能做
									;
							}
						}
					}
				}
			}
		});
	}

	public void destroy() {
		this.button.getParent().remove(this.button);
		Main.all.remove(this);
	}

	protected Point getPoint() {
		return point;
	}

	public void changeStatus(boolean sendCommand) {
		if (status == null) {
			this.button.setBackground(Color.black);
			this.button.setText("");
			this.status = QiZiStatus.扣着的;
		} else if (QiZiStatus.扣着的.equals(this.status)) {
			this.button.setBackground(Color.LIGHT_GRAY);
			this.button.setText(this.name.toString());
			this.status = QiZiStatus.掀开的;
			if (Main.me.color == null) {
				Main.setColor(this.color);
				logger.info("决定了" + Main.me + "的颜色为" + this.color);
				if (sendCommand)
					Main.write(Operation.定色 + ":[" + this.point.x + ","
							+ this.point.y + "]");
			}
			if (sendCommand)
				Main.write(Operation.翻子 + ":[" + this.point.x + ","
						+ this.point.y + "]");
			UIExtends.unflash(this.button);
			Main.currentSelectQizi = null;
		} else if (QiZiStatus.掀开的.equals(this.status)&&this.isMoveable()==true) {
			if (Main.me.color.equals(this.color)) {
				UIExtends.flashComponent(this.button, Color.green, 200);
				this.status = QiZiStatus.闪烁的;
				Main.currentSelectQizi = this;
			}
		} else if (QiZiStatus.闪烁的.equals(this.status)) {
			UIExtends.unflash(this.button);
			this.status = QiZiStatus.掀开的;
			Main.currentSelectQizi = null;
		}
	}

	public static List<QiZi> generateAll(Color color) {
		List<QiZi> list = new ArrayList<QiZi>();
		QiZiName names[] = QiZiName.values();
		for (QiZiName name : names) {
			for (int i = 0; i < name.getCount(); i++) {
				QiZi qz = new QiZi(name, color);
				list.add(qz);
			}
		}
		return list;
	}

	public String toString() {
		return "[" + name + "，" + color + "，" + button + "，" + point + "]";
	}

	public static void main(String[] args) {
		List<QiZi> list = generateAll(Color.red);
		for (QiZi qz : list) {
			System.out.println(qz);
		}
	}

	public void moveTo(Point point) {
		this.button.setLocation(Main.qp.qipan[point.x][point.y][0],
				Main.qp.qipan[point.x][point.y][1]);
		this.point = point;
		UIExtends.unflash(this.button);
	}

	public static QiZi get(int row, int col) {
		for (QiZi qz : Main.all) {
			if (qz.point.x == row && qz.point.y == col)
				return qz;
		}
		return null;
	}

	public boolean isMoveable() {
		for (QiZiName moveable : QiZiName.moveables) {
			if (moveable.equals(this.name))
				return true;
		}
		return false;
	}

	public boolean isBomb() {
		if (this.name.equals(QiZiName.炸弹))
			return true;
		else
			return false;
	}

	public boolean isMine() {
		if (this.name.equals(QiZiName.地雷))
			return true;
		else
			return false;
	}

	public boolean isFlag() {
		if (this.name.equals(QiZiName.军旗))
			return true;
		else
			return false;
	}

	public static boolean isAnyMineLeft(Color color) {
		for (QiZi qz : Main.all) {
			if (qz.name.equals(QiZiName.地雷))
				return true;
		}
		return false;
	}

	public boolean isInSafeHouse() {
		int x = this.point.x;
		int y = this.point.y;
		return Main.qp.isSafeHouse(x, y);
	}

	public boolean isFankai() {
		if(this.status.equals(QiZiStatus.掀开的))
			return true;
		return false;
	}
}
